package com.teamwhole.modules.grid.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.github.pagehelper.Page;
import com.google.common.collect.Maps;
import com.teamwhole.common.model.ResultInfo;
import com.teamwhole.modules.grid.entity.Facility;
import com.teamwhole.modules.grid.service.FacilityService;


@Component
@Path("/gcb/facility")
@Consumes("application/json;charset=utf-8")
@Produces("application/json;charset=utf-8")
public class FacilityResource {
	
	@Autowired
	private FacilityService facilityService;
	
	//在列表中显示所有数据
	@Path("/findList")
	@POST
	public Object findByLoginName(Facility entity) {
		Page<Facility> page = facilityService.findPage(entity);
		Map<String,Object> map = Maps.newHashMap();
		map.put("total", page.getTotal());
		map.put("rows", page.getResult());
		return map; 
	}
	
	//返回区域内的设备
	@Path("/findListByRegion")
	@POST
	public List<Facility> findByRegion(Facility entity) {
		return facilityService.findListByRegion(entity);
	}
	
	//返回某各广场内的所拥有的设备类型
	@Path("/findRegionTypesList")
	@GET
	public List<Facility> findTypes(@QueryParam("id") String id) {
		return facilityService.findRegionTypes(id);
	}
	
	//返回一个
	@Path("/get")
	@GET
	public Facility get(@QueryParam("id") String id) {
		return facilityService.get(id);
	}
	
	//增加一个 
	@Path("/create")
	@POST
	public ResultInfo create(Facility model) {
		facilityService.save(model);
		return ResultInfo.ok();
	}

	// 更新
	@Path("/update")
	@POST
	public ResultInfo update(Facility model) {
		facilityService.save(model);
		return ResultInfo.ok();
	}

	// 删除
	@Path("/delete")
	@GET
	public ResultInfo delete(@QueryParam("id") String id) {
		facilityService.delete(id);
		return ResultInfo.ok();
	}
	
	@Path("/findGroupByType")
	@POST
	public List<Map<String , List<Facility>>> findGroupByType(Facility model) {
		List<Facility> list = facilityService.findList(model);
		List<Facility> list1 = facilityService.findRegionTypes(model.getGridID());
		List<Map<String , List<Facility>>> newList = new ArrayList<Map<String , List<Facility>>>();
		if(list1 != null && list1.size()>0){
			for(Facility entity : list1){
				Map<String , List<Facility>> map = new HashMap<String, List<Facility>>();
				if(list != null && list.size()>0){
					List<Facility> l = new ArrayList<Facility>();
					for(Facility f : list){
						if(entity.getType()!=null && entity.getType()!="" &&entity.getType().equals(f.getType())){
							l.add(f);
						}
					}
					map.put(entity.getName(), l);
				}
				newList.add(map);
			}
		}
		return newList;
	}
}
